package com.shine.icis.action;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.UUID;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.cotrun.web.upload.FileDispatcher;
import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IDataSubmitService;
import com.shine.icis.blo.IRptA13Service;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.dto.RptA13;
import com.shine.icis.dto.RptA13.RptA13Id;
import com.shine.icis.exception.ICISException;
import com.shine.icis.hbean.RptA13Bak;
import com.shine.icis.hbean.SubmitConf;
import com.shine.icis.util.EFRameInf;

/**
 * 报送数据调整
 * @author zhangxr
 *
 */
public class RptA13Action extends BaseAction {

	private static final long serialVersionUID = 7638121893867812633L;
	
	private IRptA13Service rptA13Service;
	private IDataSubmitService dataSubmitService;
	//input param
	private int pageNo;
	private int pageSize;
	
	private String oldCompanyCode;
	private String oldTradeDate;
	private String oldArapType;
	private String oldOppPar;
	private String oldMktCodeSipf;
	private String oldArapBusiType;
	private String oldCurTypeSipf;
	private String oldHarapDate;
	
	private String companyCode; //证券公司代码
	private String tradeDate;//交易日期
	private String arapType;//应收应付类型
	private String oppPar;//对方机构
	private String mktCodeSipf;//市场代码
	private String arapBusiType;//业务类型
	private String curTypeSipf;//币种类别
	private String arapReason;//应收付原因
	private double arapAmt;//应收应付金额
	private String harapDate;//已收付日期
	private String arapDate;//应收付日期
	private String summary;//摘要信息
	private String busiDate;//业务日期
	private String opType;//操作类型
	private String expType;//导出方式
	
	//output param
	private String currentBusiDate;//当前交易日期
	private Collection<SubmitConf> arapReasonConfs;//A13应收付原因
	private Collection<SubmitConf> curTypeSipfConfs;//A13币种
	private Collection<SubmitConf> oppParConfs;//A13对方机构
	private Collection<SubmitConf> mktCodeSipfConfs;//A13市场代码
	private Collection<SubmitConf> arapBusiTypeConfs;//A13业务类型
	private Collection<SubmitConf> arapTypeConfs;//A13应收应付类型
	private String newCompanyCode; //新增的时候加载的证券公司编码
	
	private ListPage<RptA13> rptA13Page;
	private ListPage<RptA13Bak> rptA13BakPage;
	private String downloadUrl;//下载地址
	
	
	public void setRptA13Service(IRptA13Service rptA13Service) {
		this.rptA13Service = rptA13Service;
	}
	public void setDataSubmitService(IDataSubmitService dataSubmitService) {
		this.dataSubmitService = dataSubmitService;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	public void setOldCompanyCode(String oldCompanyCode) {
		this.oldCompanyCode = oldCompanyCode;
	}
	public void setOldTradeDate(String oldTradeDate) {
		this.oldTradeDate = oldTradeDate;
	}
	public void setOldArapType(String oldArapType) {
		this.oldArapType = oldArapType;
	}
	public void setOldOppPar(String oldOppPar) {
		this.oldOppPar = oldOppPar;
	}
	public void setOldMktCodeSipf(String oldMktCodeSipf) {
		this.oldMktCodeSipf = oldMktCodeSipf;
	}
	public void setOldArapBusiType(String oldArapBusiType) {
		this.oldArapBusiType = oldArapBusiType;
	}
	public void setOldCurTypeSipf(String oldCurTypeSipf) {
		this.oldCurTypeSipf = oldCurTypeSipf;
	}
	public void setOldHarapDate(String oldHarapDate) {
		this.oldHarapDate = oldHarapDate;
	}
	
	
	public void setCompanyCode(String companyCode) {
		this.companyCode = companyCode;
	}
	public void setTradeDate(String tradeDate) {
		this.tradeDate = tradeDate;
	}
	public void setArapType(String arapType) {
		this.arapType = arapType;
	}
	public void setOppPar(String oppPar) {
		this.oppPar = oppPar;
	}
	public void setMktCodeSipf(String mktCodeSipf) {
		this.mktCodeSipf = mktCodeSipf;
	}
	public void setArapBusiType(String arapBusiType) {
		this.arapBusiType = arapBusiType;
	}
	public void setCurTypeSipf(String curTypeSipf) {
		this.curTypeSipf = curTypeSipf;
	}
	public void setArapReason(String arapReason) {
		this.arapReason = arapReason;
	}
	public void setArapAmt(double arapAmt) {
		this.arapAmt = arapAmt;
	}
	public void setHarapDate(String harapDate) {
		this.harapDate = harapDate;
	}
	public void setArapDate(String arapDate) {
		this.arapDate = arapDate;
	}
	public void setSummary(String summary) {
		this.summary = summary;
	}
	public void setBusiDate(String busiDate) {
		this.busiDate = busiDate;
	}
	public void setOpType(String opType) {
		this.opType = opType;
	}
	public void setExpType(String expType) {
		this.expType = expType;
	}
	public String getCurrentBusiDate() {
		return currentBusiDate;
	}
	
	public Collection<SubmitConf> getArapReasonConfs() {
		return arapReasonConfs;
	}
	
	public Collection<SubmitConf> getCurTypeSipfConfs() {
		return curTypeSipfConfs;
	}
	
	public Collection<SubmitConf> getOppParConfs() {
		return oppParConfs;
	}
	
	public Collection<SubmitConf> getMktCodeSipfConfs() {
		return mktCodeSipfConfs;
	}
	
	
	public Collection<SubmitConf> getArapBusiTypeConfs() {
		return arapBusiTypeConfs;
	}
	
	public Collection<SubmitConf> getArapTypeConfs() {
		return arapTypeConfs;
	}
	
	public String getNewCompanyCode() {
		return newCompanyCode;
	}
	public ListPage<RptA13> getRptA13Page() {
		return rptA13Page;
	}
	public ListPage<RptA13Bak> getRptA13BakPage() {
		return rptA13BakPage;
	}
	public String getDownloadUrl() {
		return downloadUrl;
	}
	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryA13() {
		currentBusiDate = dataSubmitService.getBusiDate();
		
		arapReasonConfs = SysCodeDictLoader.getInstance().getArapReasonConfs();
		curTypeSipfConfs = SysCodeDictLoader.getInstance().getCurTypeSipfConfs();
		oppParConfs = SysCodeDictLoader.getInstance().getOppParConfs();
		mktCodeSipfConfs = SysCodeDictLoader.getInstance().getMktCodeSipfConfs();
		arapBusiTypeConfs = SysCodeDictLoader.getInstance().getArapBusiTypeConfs();
		arapTypeConfs = SysCodeDictLoader.getInstance().getAllArapTypeConfs();
		newCompanyCode = this.rptA13Service.getNewCompanyCode(currentBusiDate.trim().replaceAll("-", ""));
		
		return success();
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String queryRptA13() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		try {
			rptA13Page = rptA13Service.getRptA13Page(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	
	/**
	 * 导出Excel
	 */
	public String exportRptA13() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters qp = new QueryParameters();
		qp.addParameter("busiDate", Integer.parseInt(busiDate.trim().replaceAll("-", "")));
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = rptA13Service.csExpDataRptA13(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
		
	
	/**
	 * 新增
	 * @return
	 */
	public String addRptA13() {
		if (tradeDate == null || tradeDate.trim().equals("")
				|| arapType == null || arapType.trim().equals("")
				|| oppPar == null || oppPar.trim().equals("")
				|| mktCodeSipf == null || mktCodeSipf.trim().equals("")
				|| arapBusiType == null || arapBusiType.trim().equals("")
				|| curTypeSipf == null || curTypeSipf.trim().equals("")
				|| arapReason == null || arapReason.trim().equals("")
				|| harapDate == null || harapDate.trim().equals("")
//				|| arapDate == null || arapDate.trim().equals("")
				|| busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
		try {
			//对页面传过来的日期进行格式化
			String _arapDate = arapDate == null || arapDate.trim().equals("") ? null : arapDate.trim().replaceAll("-", "");
			
			RptA13 rptA13 = new RptA13();
			RptA13Id id = rptA13.new RptA13Id();
			id.setTradeDate(tradeDate.trim().replaceAll("-", ""));
			id.setArapType(arapType);
			id.setOppPar(oppPar);
			id.setMktCodeSipf(mktCodeSipf);
			id.setArapBusiType(arapBusiType);
			id.setCurTypeSipf(curTypeSipf);
			id.setHarapDate(harapDate.trim().replaceAll("-", ""));
			id.setBusiDate(busiDate.trim().replaceAll("-", ""));
			
			rptA13.setId(id);
			rptA13.setArapReason(arapReason);
			rptA13.setArapAmt(arapAmt);
			rptA13.setArapDate(_arapDate);
			rptA13.setSummary(summary);
			
			String userCode = user.getUSER_CODE();
			this.rptA13Service.addRptA13(rptA13,userCode);
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(),EFRameInf.FUNCCODE_RPT, "增加证券公司资金报送数据微调A13", success);
		}
	}
	/**
	 * 修改A13信息
	 * @author zhangxr
	 * @return
	 */
	public String modifyRptA13() {
		if (oldCompanyCode == null || oldCompanyCode.trim().equals("")
				|| oldTradeDate == null || oldTradeDate.trim().equals("") 
				|| oldArapType == null || oldArapType.trim().equals("")
				|| oldOppPar == null || oldOppPar.trim().equals("")
				|| oldMktCodeSipf == null || oldMktCodeSipf.trim().equals("")
				|| oldArapBusiType == null || oldArapBusiType.trim().equals("")
				|| oldCurTypeSipf == null || oldCurTypeSipf.trim().equals("")
				|| oldHarapDate == null || oldHarapDate.trim().equals("")
				|| companyCode == null || companyCode.trim().equals("")
				|| tradeDate == null || tradeDate.trim().equals("")
				|| arapType == null || arapType.trim().equals("")
				|| oppPar == null || oppPar.trim().equals("")
				|| mktCodeSipf == null || mktCodeSipf.trim().equals("")
				|| arapBusiType == null || arapBusiType.trim().equals("")
				|| curTypeSipf == null || curTypeSipf.trim().equals("")
				|| arapReason == null || arapReason.trim().equals("")
				|| harapDate == null || harapDate.trim().equals("")
//				|| arapDate == null || arapDate.trim().equals("")
				|| busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
		try {
			//对页面传过来的日期进行格式化
			String _arapDate = arapDate == null || arapDate.trim().equals("") ? null : arapDate.trim().replaceAll("-", "");
			
			//封装新的A13信息
			RptA13 rptA13 = new RptA13();
			RptA13Id id = rptA13.new RptA13Id();
			id.setCompanyCode(companyCode);
			id.setTradeDate(tradeDate.trim().replaceAll("-", ""));
			id.setArapType(arapType);
			id.setOppPar(oppPar);
			id.setMktCodeSipf(mktCodeSipf);
			id.setArapBusiType(arapBusiType);
			id.setCurTypeSipf(curTypeSipf);
			id.setHarapDate(harapDate.trim().replaceAll("-", ""));
			id.setBusiDate(busiDate.trim().replaceAll("-", ""));
			
			rptA13.setId(id);
			rptA13.setArapReason(arapReason);
			rptA13.setArapAmt(arapAmt);
			rptA13.setArapDate(_arapDate);
			rptA13.setSummary(summary);
			
			//封装原先A13主键的相关信息
			RptA13Id oldId = rptA13.new RptA13Id();
			oldId.setCompanyCode(oldCompanyCode);
			oldId.setTradeDate(oldTradeDate.trim().replaceAll("-", ""));
			oldId.setArapType(oldArapType);
			oldId.setOppPar(oldOppPar);
			oldId.setMktCodeSipf(oldMktCodeSipf);
			oldId.setArapBusiType(oldArapBusiType);
			oldId.setCurTypeSipf(oldCurTypeSipf);
			oldId.setHarapDate(oldHarapDate.trim().replaceAll("-", ""));
			oldId.setBusiDate(busiDate.trim().replaceAll("-", ""));
			
			String userCode = user.getUSER_CODE();
			this.rptA13Service.modifyRptA13(oldId, rptA13,userCode);
			success = true;
			return success();
		} catch(ICISException e) {
			return error(e.getMessage());
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_RPT, "修改证券公司资金报送数据微调A13", success);
		}
	}	
	
	/**
	 * 删除
	 * @return
	 */
	public String deleteRptA13() {
		if (companyCode == null || companyCode.trim().equals("")
				||arapType == null || arapType.trim().equals("")
				|| tradeDate == null || tradeDate.trim().equals("")
				|| oppPar == null || oppPar.trim().equals("")
				|| mktCodeSipf == null || mktCodeSipf.trim().equals("")
				|| arapBusiType == null || arapBusiType.trim().equals("")
				|| curTypeSipf == null || curTypeSipf.trim().equals("")
				|| harapDate == null || harapDate.trim().equals("")) {
			return error("参数不能为空");
		}
		boolean success = false;
		SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
		try {
			RptA13Id id = new RptA13().new RptA13Id();
			id.setCompanyCode(companyCode);
			id.setTradeDate(tradeDate.trim().replaceAll("-", ""));
			id.setArapType(arapType);
			id.setOppPar(oppPar);
			id.setMktCodeSipf(mktCodeSipf);
			id.setArapBusiType(arapBusiType);
			id.setCurTypeSipf(curTypeSipf);
			id.setHarapDate(harapDate.trim().replaceAll("-", ""));
			id.setBusiDate(busiDate.trim().replaceAll("-", ""));
			
			String userCode = user.getUSER_CODE();
			this.rptA13Service.deleteRptA13(id,userCode);
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_RPT, "删除证券公司资金报送数据微调A13", success);
		}
	}

	/**
	 * 转到查询页面
	 * @return
	 */
	public String initQueryA13Log() {
		currentBusiDate = dataSubmitService.getBusiDate();
		return success();
	}
	
	/**
	 * 查询
	 * @return
	 */
	public String queryRptA13Log() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters qp = new QueryParameters();
		//设置分页信息
		qp.setPageNo(pageNo);
		qp.setPageSize(pageSize);
		qp.addOrder("id", true);
		qp.addParameter("busiDate", busiDate.trim().replaceAll("-", ""));
		if (opType != null && !opType.equals("")) {
			qp.addParameter("opType", opType);
		}
		try {
			rptA13BakPage = rptA13Service.getRptA13BakPage(qp);
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	
	/**
	 * 导出Excel
	 */
	public String exportRptA13Log() {
		if (busiDate == null || busiDate.trim().equals("")) {
			return error("参数不能为空");
		}
		QueryParameters qp = new QueryParameters();
		qp.addOrder("oprDateTime", false);
		qp.addParameter("busiDate", busiDate.trim().replaceAll("-", ""));
		if (opType != null && !opType.equals("")) {
			qp.addParameter("opType", opType);
		}
		try {
			//生成一个随机文件夹保证文件不重复
			String randomDir = UUID.randomUUID().toString() + "/"; 
			String saveDir = FileDispatcher.getTempDir() + randomDir;//文件保存绝对路径
			String absDir = FileDispatcher.getTempAbsDir() + randomDir;//WEB访问的相对路径
			String temFileName = rptA13Service.csExpDataRptA13Bak(qp, saveDir, expType);
			downloadUrl = absDir + URLEncoder.encode(temFileName, SysConstants.DOWNLOAD_FILENAME_ENCODING);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return success();
	}
}
